package com.htdc.web.controller;

import com.htdc.web.bean.Agreement;
import com.htdc.web.bean.Company;
import com.htdc.web.bean.ProjectTb;
import com.htdc.web.bean.Room;
import com.htdc.web.service.AgreementService;
import com.htdc.web.service.CountService;
import com.htdc.web.service.ProjectService;
import com.htdc.web.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Sfx
 * Date: 2018/9/6 0006
 * Time: 下午 2:57
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    RoomService roomService;
    @Autowired
    CountService countService;
    @Autowired
    ProjectService projectService;
    @Autowired
    AgreementService agreementService;
    //跳转资源管理首页
    @RequestMapping("/toResourceMgr")
    public String toResourceMgr(Model model){
        List<Company> companies = countService.getAllCompanyData();
        HashMap<Company,List<ProjectTb>> CPMap = new HashMap<>();
        for (Company company : companies) {
            CPMap.put(company,projectService.getProjectByCid((int)company.getCid()));
        }
        model.addAttribute("CPMap",CPMap);
        return "roomMgr/roomIndex";
    }
    //根据选择的项目显示房间资源
    @RequestMapping("/getRooms")
    public String showRooms(int pid,Model model,HttpSession session){
        ProjectTb project = projectService.getProject(pid);
        List<Room> rooms = roomService.getRoomsByPid(pid);
        session.setAttribute("pid",pid);
        session.setAttribute("rooms",rooms);
        return "house_list";
    }
    //跳转修改房源页面
    @RequestMapping(value = "/toChange")
    public String toChange(int rid,Model model){
        Room room = roomService.getRoomByRid(rid);
        model.addAttribute("room",room);
        return "house_edit";
    }

    //修改房源信息
    @RequestMapping(value = "/changeRoom",method = RequestMethod.POST)
    @ResponseBody
    public Object changeRoom(Room room){
        roomService.changeRoom(room.getRoomArea(),room.getRoomRealArea(),room.getRname(),(int)room.getRid());
        return "success";
    }

    @RequestMapping(value = "/toAddRoom")
    public String toAddRoom(){
        return "house_add";
    }

    @RequestMapping(value = "/addRoom",method = RequestMethod.POST)
    @ResponseBody
    public Object addRoom(Room room,HttpSession session){
        int pid = (int)session.getAttribute("pid");

        ProjectTb project = projectService.getProject(pid);
        System.err.println(pid+""+project);
        roomService.addRoom(room.getRname(),room.getRoomArea(),room.getRoomRealArea(),(int)project.getPid(),"待出租",(int)project.getCid());
        return "success";
    }
    //列出空闲资产
    @RequestMapping("/leisureRoomlist")
    public String leisureRoomList(Model model){
        List<Room> leisureRooms=roomService.getUnrentRooms();
        model.addAttribute("leisureRooms",leisureRooms);
        return "leisureRoom_list";
    }

    //历史合同
    @RequestMapping("/showHistory")
    public String historyAgreement(int rid,Model model){
        Room room = roomService.getRoomByRid(rid);
        List<Agreement> agreements = agreementService.show(rid);
        model.addAttribute("agreements",agreements);
        return "room_history";
    }

}
